package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;
    /**
     * 添加商机
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Business business) {
        log.info("添加商机{}",business);
        //补全信息
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    /**
     * 查询商机列表
     */
    @GetMapping
    public Result list(BusinessDto businessDto) {
        log.info("查询商机{}", businessDto);
        PageResult<Business> page = businessService.listBusiness(businessDto);
        return Result.success(page);
    }

    /**
     * 商机分配
     */
    @Log
    @PutMapping("assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("商机分配{},{}", businessId, userId);
        Business buainess = new Business();
        buainess.setId(businessId);
        buainess.setUserId(userId);
        buainess.setStatus(2);
        buainess.setUpdateTime(LocalDateTime.now());
        businessService.updateById(buainess);
        return Result.success();
    }

    /**
     * 根据Id查询 商机
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询商机:{}", id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }

    /**
     * 跟进商机
     */
    @Log
    @PutMapping
    public Result trackBusiness(@RequestBody Business business) {
        log.info("跟进商机:{}", business);
        businessService.trackBusiness(business);
        return Result.success();
    }

    /**
     * 踢回公海
     */
    @Log
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        log.info("踢回公海:{}", id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }

    /**
     * 商机转客户
     */
    @Log
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("商机转客户:{}", id );
        businessService.toCustomer(id);
        return Result.success();
    }

    /**
     *公海池列表查询
     */
    @GetMapping("/pool")
    public Result listPool(BusinessDto businessDto) {
        log.info("公海池列表查询:{}", businessDto);
        PageResult<Business> page = businessService.listBusinessPool(businessDto);
        return Result.success(page);
    }
}
